#include"comm.hpp"

//对管道文件进行管理
int main()
{
    bool r = createFifo(NAMED_PID);
    assert(r);
    (void)r; // 显式忽略 r，避免编译器警告

    //打开文件
    // std::cout<< "server begin"<<std::endl;
    int rfd = open(NAMED_PID,O_RDONLY);//已只读的方式打开
    // std::cout<< "server end"<<std::endl;
    if(rfd <  0)//打开失败直接退出
    {
        exit(1);
    }

    //read

    char buffer[1024];
    while(true)
    {
        ssize_t s = read(rfd , buffer , sizeof(buffer) - 1 );
        if(s > 0 )//正常读取
        {
            buffer[s] = 0;
            std::cout<< "client->serve #" << buffer << std::endl;
        }
        else if(s==0)
        {
            std::cout<<"client quit , me too!"<< std::endl;
            break;
        }
        else
        {
            std::cout<<"err string :"<< strerror(errno) << std::endl;
            break;
        }
    }

    close(rfd);
    
    
    //sleep(10);
    removeFifo(NAMED_PID);
    
    return 0;
}